package com.unicom.mec.device.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unicom.mec.device.common.ResultBean;
import com.unicom.mec.device.remote.UserRemote;
import com.unicom.mec.device.service.EdgeEndpointService;
import com.unicom.mec.device.service.EdgeNodeService;
import com.unicom.mec.device.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 边缘节点控制层
 *
 * @author 刘亮武
 * @date 2020年06月17日
 */
@Api(value = "/edge-node", tags = "边缘节点管理")
@RestController
@RequestMapping("/edge-node")
public class EdgeNodeController {

    @Autowired
    private EdgeNodeService edgeNodeService;
    @Autowired
    private EdgeEndpointService edgeEndpointService;
    @Autowired
    private UserRemote userRemote;

    @ApiOperation(value = "边缘节点统计信息", httpMethod = "GET")
    @GetMapping("/statistic/")
    @ResponseBody
    ResultBean<EdgeNodeStatisticVo> findEdgeNodeStatistic(@ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId) {
        EdgeNodeStatisticVo edgeNodeStatisticVo = edgeNodeService.findEdgeNodeStatistic(tenantId);
        ResultBean<EdgeNodeStatisticVo> result = new ResultBean<>();
        result.setData(edgeNodeStatisticVo);
        return result;
    }

    @ApiOperation(value = "查询边缘节点信息", httpMethod = "GET")
    @GetMapping("/")
    @ResponseBody
    ResultBean<List<EdgeNodeVo>> findEdgeNodes(@ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId) {
        List<EdgeNodeVo> edgeNodeVos = edgeNodeService.findEdgeNodes(tenantId);
        List<Long> ids = edgeNodeVos.stream().map(EdgeNodeVo::getTenantId).collect(Collectors.toList());
        ResultBean<List<TenantVo>> resultBean = userRemote.findTenantByIds(ids.toArray(new Long[]{}));
        Map<Long, String> tenantMap = resultBean.getData().stream().collect(Collectors.toMap(TenantVo::getId, TenantVo::getUserName));
        edgeNodeVos.forEach(edgeNodeVo -> edgeNodeVo.setTenantName(tenantMap.get(edgeNodeVo.getTenantId())));
        ResultBean<List<EdgeNodeVo>> result = new ResultBean<>();
        result.setData(edgeNodeVos);
        return result;
    }

    @ApiOperation(value = "已接入边缘节点列表", httpMethod = "GET")
    @GetMapping("/access/")
    @ResponseBody
    ResultBean<Page<EdgeNodeVo>> findAccessEdgeNodes(@ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId,
                                                       @ApiParam(value = "页码") @RequestParam(value = "page") Integer page,
                                                       @ApiParam(value = "查询条数") @RequestParam(value = "pageSize") Integer pageSize) {
        Page<EdgeNodeVo> pageVo = edgeNodeService.findAccessEdgeNodes(tenantId,page,pageSize);
        setTenantInfo(pageVo);
        ResultBean<Page<EdgeNodeVo>> result = new ResultBean<>();
        result.setData(pageVo);
        return result;
    }

    @ApiOperation(value = "未接入边缘节点列表", httpMethod = "GET")
    @GetMapping("/noAccess/")
    @ResponseBody
    ResultBean<Page<EdgeNodeVo>> findNoAccessEdgeNodes(@ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId,
                                                       @ApiParam(value = "页码") @RequestParam(value = "page") Integer page,
                                                       @ApiParam(value = "查询条数") @RequestParam(value = "pageSize") Integer pageSize) {
        Page<EdgeNodeVo> pageVo = edgeNodeService.findNoAccessEdgeNodes(tenantId,page,pageSize);
        setTenantInfo(pageVo);
        ResultBean<Page<EdgeNodeVo>> result = new ResultBean<>();
        result.setData(pageVo);
        return result;
    }

    private void setTenantInfo(Page<EdgeNodeVo> pageVo) {
        List<Long> ids = pageVo.getRecords().stream().map(EdgeNodeVo::getTenantId).collect(Collectors.toList());
        ResultBean<List<TenantVo>> resultBean = userRemote.findTenantByIds(ids.toArray(new Long[]{}));
        Map<Long, String> tenantMap = resultBean.getData().stream().collect(Collectors.toMap(TenantVo::getId, TenantVo::getUserName));
        pageVo.getRecords().forEach(edgeNodeVo -> edgeNodeVo.setTenantName(tenantMap.get(edgeNodeVo.getTenantId())));
    }

    @ApiOperation(value = "边缘节点信息", httpMethod = "GET")
    @GetMapping("/{id}")
    @ResponseBody
    ResultBean<EdgeNodeVo> findEdgeNodeById(@ApiParam(value = "主键") @PathVariable(value = "id") Long id) {
        EdgeNodeVo edgeNodeVo = edgeNodeService.findEdgeNodeById(id);
        ResultBean<TenantVo> resultBean = userRemote.findTenantById(edgeNodeVo.getTenantId());
        edgeNodeVo.setTenantName(resultBean.getData().getUserName());
        ResultBean<EdgeNodeVo> result = new ResultBean<>();
        result.setData(edgeNodeVo);
        return result;
    }

    @ApiOperation(value = "边缘节点终端列表", httpMethod = "GET")
    @GetMapping("/{edgeNodeId}/edge-endpoints")
    @ResponseBody
    ResultBean<List<EdgeEndpointVo>> findEdgeEndpointForEdgeNode(@ApiParam(value = "边缘节点主键") @PathVariable(value = "edgeNodeId") Long edgeNodeId) {
        List<EdgeEndpointVo> edgeEndpointVos = edgeEndpointService.findEdgeEndpoints(null,edgeNodeId);
        ResultBean<List<EdgeEndpointVo>> result = new ResultBean<>();
        result.setData(edgeEndpointVos);
        return result;
    }

    @ApiOperation(value = "边缘节点修改", httpMethod = "PUT")
    @PutMapping("/")
    @ResponseBody
    ResultBean editEdgeNode(@RequestBody @Valid EditEdgeNodeVo editEdgeNodeVo) {
        edgeNodeService.editEdgeNode(editEdgeNodeVo);
        ResultBean result = new ResultBean<>();
        return result;
    }
}
